#include <stdio.h>
#include <conio.h>
#include <limits.h>
#include "FONT.H"
#include "INPUT.H"

#define FPS 30
#define DELTA 1000/FPS

#define CANVAS_WIDTH 1024
#define CANVAS_HEIGHT 768

int main()
{
	int CLIP_WIDTH = 320, CLIP_HEIGHT = 200;
	Line line1, line2, line3, line4, temp;	
	boolean visible;
	Region clipPlane;	
	int posX;
	int x = 0, y = 0;
	int tInput;	
	Fonts hollywood;
	boolean valid = loadFonts("FONT.FNT", &hollywood);	
	
	line1.x0 = 0;
	line1.y0 = 0;
	line1.x1 = CANVAS_WIDTH-1;
	line1.y1 = CANVAS_HEIGHT-1;
	
	line2.x0 = CANVAS_WIDTH-1;
	line2.y0 = 0;
	line2.x1 = 0;
	line2.y1 = CANVAS_HEIGHT-1;
	
	line3.x0 = (CANVAS_WIDTH>>1);
	line3.y0 = 0;
	line3.x1 = (CANVAS_WIDTH>>1);
	line3.y1 = CANVAS_HEIGHT-1;
	
	line4.x0 = 0;
	line4.y0 = (CANVAS_HEIGHT>>1);
	line4.x1 = CANVAS_WIDTH-1;
	line4.y1 = (CANVAS_HEIGHT>>1);
	
	setMode(VGA_MODE);	
	while(TRUE){
		if(kbhit()){
			tInput = getKey();
			if(tInput == 27){
				break;
			}		
			switch (tInput) {
				case 328 : /*UP*/
					if(y>0)
					{
						y--;
					}
					break;
				case 331 : /*LEFT*/
					if(x>0)
					{
						x--;
					}
					break;
				case 333 : /*RIGHT*/
					if(x+CLIP_WIDTH<CANVAS_WIDTH)
					{
						x++;
					}
					break;
				case 336 : /*DOWN*/
					if(y+CLIP_HEIGHT<CANVAS_HEIGHT)
					{
						y++;
					}
					break;
				case 43:
					if((CLIP_HEIGHT > 25) && (CLIP_WIDTH > 40))
					{
						CLIP_HEIGHT/=2;
						CLIP_WIDTH/=2;
					}		
					break;
				case 45:
					if((CLIP_HEIGHT < (INT_MAX>>1)) && (CLIP_WIDTH < (INT_MAX>>1)))
					{
						CLIP_HEIGHT*=2;
						CLIP_WIDTH*=2;
					}					
					break;
			}
		}
		
		clipPlane.left = x;
		clipPlane.right = x+CLIP_WIDTH-1;
		clipPlane.top = y;
		clipPlane.bottom = y+CLIP_HEIGHT-1;
		
		setBackground(LIGHT_BLUE);
		
		if(valid)
		{
			posX = 100;
			drawClippedScreenFont(hollywood, 'G', posX,10, BROWN, clipPlane);
			posX += hollywood.defaultSymbol.width;
			drawClippedScreenFont(hollywood, 'r', posX,10, BROWN, clipPlane);
			posX += hollywood.defaultSymbol.width;
			drawClippedScreenFont(hollywood, 'a', posX,10, BROWN, clipPlane);
			posX += hollywood.defaultSymbol.width;
			drawClippedScreenFont(hollywood, 'f', posX,10, BROWN, clipPlane);
			posX += hollywood.defaultSymbol.width;
			drawClippedScreenFont(hollywood, 'i', posX,10, BROWN, clipPlane);
			posX += hollywood.defaultSymbol.width;
			drawClippedScreenFont(hollywood, 'k', posX,10, BROWN, clipPlane);
			posX += hollywood.defaultSymbol.width;
			drawClippedScreenFont(hollywood, 'a', posX,10, BROWN, clipPlane);			
		}
		
		visible = clipLineToScreen(line1, &temp, clipPlane);
		if(visible) drawLine(temp.x0, temp.y0, temp.x1, temp.y1, CYAN);
		
		visible = clipLineToScreen(line2, &temp, clipPlane);
		if(visible) drawLine(temp.x0, temp.y0, temp.x1, temp.y1, MAGENTA);
		
		visible = clipLineToScreen(line3, &temp, clipPlane);
		if(visible) drawLine(temp.x0, temp.y0, temp.x1, temp.y1, YELLOW);
		
		visible = clipLineToScreen(line4, &temp, clipPlane);
		if(visible) drawLine(temp.x0, temp.y0, temp.x1, temp.y1, BLACK);
		
		redraw();
		delay(DELTA);
	}
	freeFonts(&hollywood);
	setMode(TEXT_MODE);
	return EXIT_SUCCESS;
}
